package test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Wyndem
 * @Description: 天气站，可检测当前的温度，湿度
 * @Date: Created in  2019-01-20 19:54
 * @Modified By:
 */
public class WeatherStation implements Subject {

    private List<Observers> observersList;


    //温度
    private Double temperature;

    private Double humidity;


    public WeatherStation() {
        observersList=new ArrayList<Observers>();
        temperature=28.0;
        humidity=55.0;
    }

    @Override
    public void removeObservers(Observers observers) {
        boolean remove = observersList.remove(observers);
        //如果没有删除成功，代表该观察者没有订阅该主题
        if (!remove)  new RuntimeException("该观察者没有订阅当前主题");
    }

    @Override
    public void addObservers(Observers observers) {
        observersList.add(observers);
    }


     /**
      * @title: 通知观察者
      * @description: 该例子演示的是主动拉取数据，而不是主动推数据
      * @author:  Wyndem
      * @date:  2019/1/20  20:02
      **/
    @Override
    public void notifyAllObservers() {
        for (Observers o :
                observersList) {
            o.updata();
        }
    }


    /**
     * 获取温度
     * @return 温度
     */
    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    /**
     * 获取湿度
     * @return 湿度
     */
    public Double getHumidity() {
        return humidity;
    }
}
